package design.simdi.code.final_paper.site.panels.menu;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.authorization.strategies.role.Roles;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;

import design.simdi.code.final_paper.site.application.ApplicationRoles;
import design.simdi.code.final_paper.site.application.MyAuthenticatedWebSession;
import design.simdi.code.final_paper.site.panels.MenuEntry;
import design.simdi.code.final_paper.site.panels.PagePanel;
import design.simdi.code.final_paper.site.panels.administrare.AdministrarePageContentPanel;
import design.simdi.code.final_paper.site.panels.blog.BlogPageContentPanel;
import design.simdi.code.final_paper.site.panels.licente.LicentePageContentPanel;
import design.simdi.code.final_paper.site.panels.proiecte.ProiectePageContentPanel;
import design.simdi.code.final_paper.site.panels.sesiuni.SesiuniPageContentPanel;
import design.simdi.code.final_paper.utils.PanelFactory;

/**
 * 
 * @author Marius Dumitriu - (Jun 5, 2010 - 9:03:25 PM)
 * 
 */
public class MenuPanel extends PagePanel {

	private static final long serialVersionUID = 1L;
	private static final String CONTENT = "content";

	private List<MenuEntry> listaMenu;
	private ListItem<MenuEntry> selectedMenuEntry;

	public MenuPanel(String id) {
		super(id);

	}

	public MenuPanel(String id, IModel<?> model) {
		super(id, model);
	}

	@Override
	public void panelInit() {
		setOutputMarkupId(true);

		ListView<MenuEntry> listaViewMenu = new ListView<MenuEntry>("menuEntries", getMenuEntries()) {

			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(final ListItem<MenuEntry> item) {
				item.setOutputMarkupId(true);

				final MenuEntry menuEntry = item.getModelObject();

				AjaxFallbackLink<String> link;
				item.add(link = new AjaxFallbackLink<String>("link") {

					/**
					 * 
					 */
					private static final long serialVersionUID = 1L;

					@SuppressWarnings("unchecked")
					@Override
					public void onClick(AjaxRequestTarget target) {
						Page page = this.getParent().getPage();

						Component panel = PanelFactory.createPageContent(menuEntry.getPanelClass(), CONTENT);
						if (panel == null)
							panel = new EmptyPanel(CONTENT);

						page.get(CONTENT).replaceWith(panel);

						selectedMenuEntry.add(new SimpleAttributeModifier("class", ""));
						item.add(new SimpleAttributeModifier("class", "selected"));

						target.addComponent(item);
						target.addComponent(selectedMenuEntry);
						target.addComponent(page.get(CONTENT));

						onMenuEntryClick(target);
						selectedMenuEntry = item;

					}

				});
				link.add(new Label("name", new ResourceModel(menuEntry.getName())));
				link.setOutputMarkupId(true);

				if (selectedMenuEntry != null && selectedMenuEntry.getIndex() == item.getIndex()) {
					item.add(new SimpleAttributeModifier("class", "selected"));
					selectedMenuEntry = item;
				}

				if (item.getIndex() == 0 && selectedMenuEntry == null) {
					selectedMenuEntry = item;
					selectedMenuEntry.setOutputMarkupId(true);
					item.add(new SimpleAttributeModifier("class", "selected"));
				}

			}
		};

		add(listaViewMenu);

	}

	public void onMenuEntryClick(AjaxRequestTarget target) {
		target.addComponent(this.getPage());
	}

	private List<MenuEntry> getMenuEntries() {
		listaMenu = new ArrayList<MenuEntry>();

		Roles roluriCurente = MyAuthenticatedWebSession.get().getRoles();

		listaMenu.add(new MenuEntry("licente", LicentePageContentPanel.class));
		listaMenu.add(new MenuEntry("administrare", AdministrarePageContentPanel.class));

		if (MyAuthenticatedWebSession.get().isSignedIn() && roluriCurente.contains(ApplicationRoles.ADMIN))
			listaMenu.add(new MenuEntry("sesiuni", SesiuniPageContentPanel.class));

		if (MyAuthenticatedWebSession.get().isSignedIn()) {
			listaMenu.add(new MenuEntry("blog", BlogPageContentPanel.class));

		}
		if (MyAuthenticatedWebSession.get().isSignedIn() && roluriCurente.contains(ApplicationRoles.PROFESSOR))
			listaMenu.add(new MenuEntry("proiecte", ProiectePageContentPanel.class));

		return listaMenu;
	}

	/**
	 * @param listaMenu
	 *            the listaMenu to set
	 */
	public void setListaMenu(List<MenuEntry> listaMenu) {
		this.listaMenu = listaMenu;
	}

	/**
	 * @return the listaMenu
	 */
	public List<MenuEntry> getListaMenu() {
		return listaMenu;
	}

}
